package com.kyle.pagingdemo.ui.adapter

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.paging.PagingDataAdapter
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.RecyclerView
import com.kyle.pagingdemo.R
import com.kyle.pagingdemo.data.model.Post
import com.kyle.pagingdemo.databinding.ItemViewPagerBinding
import javax.inject.Inject

/**
 * author: liliang
 * date: 1/20/21 9:22 PM
 * desc:
 */
class ViewPagerAdapter @Inject constructor(): PagingDataAdapter<Post, ViewPagerAdapter.ViewPagerHolder>(
    diffCallback
) {

    companion object {
        private val diffCallback = object : DiffUtil.ItemCallback<Post>() {
            override fun areItemsTheSame(oldItem: Post, newItem: Post) = oldItem == newItem

            override fun areContentsTheSame(oldItem: Post, newItem: Post) = oldItem == newItem

        }
    }

    override fun onBindViewHolder(holder: ViewPagerHolder, position: Int) {
        val item = getItem(position)
        val imageView = holder.imageView
        item?.let {
//            imageView.transitionName = imageView.context.getString(R.string.container_card_item, position)
            val imageUrl = "https://images.anime-pictures.net/${it.md5.subSequence(0, 3)}/${it.md5}${it.ext}"
            imageView.loadPhotoUrlWithThumbnail(imageUrl, it.medium_preview, it.color)
        }
    }


    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewPagerHolder {
        val binding =
            ItemViewPagerBinding.inflate(LayoutInflater.from(parent.context), parent, false)
        return ViewPagerHolder(binding)
    }


    class ViewPagerHolder(binding: ItemViewPagerBinding) : RecyclerView.ViewHolder(binding.root) {
        val imageView = binding.profileDetailBackground
    }

}